package com.dgut.smart_house.service.impl;

import com.dgut.smart_house.domain.Device;
import com.dgut.smart_house.domain.Node;
import com.dgut.smart_house.domain.Types;
import com.dgut.smart_house.dto.DataTypeDto;
import com.dgut.smart_house.dto.DeviceNodeDto;
import com.dgut.smart_house.dto.RealDataDto;
import com.dgut.smart_house.dto.UserDeviceDto;
import com.dgut.smart_house.mapper.DatasMapper;
import com.dgut.smart_house.mapper.DeviceMapper;
import com.dgut.smart_house.mapper.NodeMapper;
import com.dgut.smart_house.mapper.TypeMapper;
import com.dgut.smart_house.service.DeviceListService;
import com.dgut.smart_house.vo.DeviceDataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceListServiceImpl implements DeviceListService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DatasMapper datasMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private TypeMapper typeMapper;


    //    @Cacheable(cacheNames = "smartHome",key = "#root.method")
    @Override
    public List<UserDeviceDto> findAllDevices(Integer userId) {
        return deviceMapper.findAllDevices(userId);
    }

    @Override
    public DeviceDataVo findDeviceDataByDevId(Integer devId, Integer pageNo, Integer pageSize) {
        DeviceNodeDto deviceNodeDto = deviceMapper.findDeviceByDevId(devId);
        DeviceDataVo deviceDataVo = new DeviceDataVo();

        PageHelper.startPage(pageNo, pageSize);
        List<DataTypeDto> dataTypeDtos = datasMapper.findData2DevId(devId);
        PageInfo<DataTypeDto> page = new PageInfo<DataTypeDto>(dataTypeDtos);

        deviceDataVo.setDataTypeDtoList(page);
        deviceDataVo.setNodeName(deviceNodeDto.getNodeName());
        deviceDataVo.setNodeDesc(deviceNodeDto.getNodeDesc());
        deviceDataVo.setDeviceDesc(deviceNodeDto.getDeviceDesc());
        return deviceDataVo;
    }

    @Override
    public List<RealDataDto> findRealDatas(Integer userId) {
        return datasMapper.findRealDatas(userId);
    }

    @Override
    public Integer deleteDevice(Integer devId) {
        return deviceMapper.deleteDevice(devId);
    }

    @Override
    public int updateDevice(Integer devId, String desc) {

        Device device = deviceMapper.findDevice(devId);
        List<Device> deviceList = deviceMapper.findByNodeId(device.getNodeId());
        try {
            for (Device dev : deviceList) {
                deviceMapper.updateDevice(dev.getDevId(), desc);
            }
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int createDevice(Device device) {
        Device dev = device;
        ArrayList<Types> typesList = typeMapper.findTypeIdByNodeId(dev.getNodeId());
        if (typesList == null || typesList.size() == 0)
            return 0;
        for (int i = 0; i < typesList.size(); i++) {
            dev.setTypeId(typesList.get(i).getTypeId());
            dev.setStatus(1);
            deviceMapper.createDevice(device);
        }
        return 1;
    }

    @Override
    public Integer findUserByDevId(Integer devId) {
        return deviceMapper.findUserByDevId(devId);
    }

    @Override
    public Integer deleteDatasByDevId(Integer devId) {
        return datasMapper.deleteDatasByDevId(devId);
    }

    //    @Cacheable(cacheNames = "smartHome",key = "#root.method")
    @Override
    public List<Node> findAllNodes() {
        return nodeMapper.findAllNodes();
    }

    //    @Cacheable(cacheNames = "smartHome",key = "#root.method+#root.args")
    @Override
    public DeviceNodeDto findDeviceByDevId(Integer devId) {
        return deviceMapper.findDeviceByDevId(devId);
    }

    @Override
    public int checkDeviceStatus(int devId) {
        return deviceMapper.checkDeviceStatus(devId);
    }

    @Override
    public List<Device> findByCommand(String command) {
        return deviceMapper.findByCommand(command);
    }
}
